package com.moyq5.permit.center.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.entity.SysRole;
import com.moyq5.permit.center.bean.enums.SysPlatform;
import com.moyq5.permit.center.bean.search.SysRole4Search;
import com.moyq5.permit.center.feign.client.SysRoleFeign;
import com.moyq5.permit.center.web.MyErrorCode;
import com.moyq5.permit.center.web.MyException;
import com.moyq5.permit.center.web.jackson.filter.SysRoleFilter;

/**
 * 角色信息
 * @author Moyq5
 * @date 2019年7月16日
 */
@RestController
@RequestMapping("/role")
public class SysRoleController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(SysRoleController.class);
	
	@Autowired
	private SysRoleFeign roleFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月17日
	 * @param role4s
	 * @param pd
	 * @param platform
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/{plat}/!/**", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String page(SysRole4Search role4s, PageData pd, @PathVariable(required = false, value="plat") Integer platform) throws JsonProcessingException {
		log.debug("查询角色信息>>>page(SysRole4Search, PageData, platform)");
		if (null != platform) {
			role4s.setPlatform(SysPlatform.values()[platform]);
		}
		return toJson(roleFeign.page(new Body<SysRole4Search, PageData>(role4s, pd)), SysRoleFilter.get());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月17日
	 * @param role
	 * @param platform
	 * @return
	 */
	@PutMapping("/add/{plat}/!/**")
	@ResponseBody
	public ObjectResult<Long> add(@RequestBody SysRole role, @PathVariable("plat") int platform) {
		log.debug("添加角色信息>>>add(SysRole, platform)");
		role.setPlatform(SysPlatform.values()[platform]);
		return roleFeign.create(role);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月17日
	 * @param role
	 * @param platform
	 * @return
	 */
	@PatchMapping("/edit/{plat}/!/**")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated SysRole role, @PathVariable("plat") int platform) {
		log.debug("更新角色信息>>>edit(SysRole)");
		role.setPlatform(SysPlatform.values()[platform]);
		return roleFeign.update(role);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月17日
	 * @param roleId
	 * @param platform
	 * @return
	 */
	@DeleteMapping("/del/{plat}/!/**")
	@ResponseBody
	public StatusResult del(@RequestBody Long roleId, @PathVariable("plat") int platform) {
		log.debug("删除角色信息>>>del(roleId)");
		ObjectResult<SysRole> roleRst = roleFeign.one(roleId);
		if (roleRst.getStatus() != Status.OK) {
			return roleRst;
		}
		if (null == roleRst.getData() || roleRst.getData().getPlatform().ordinal() != platform) {
			throw new MyException(MyErrorCode.C9998);
		}
		return roleFeign.remove(roleId);
	}
	
}
